<?php

namespace app\system\controller;

use app\common\model\SiteModel;
use think\Request;

class SiteController extends BaseController
{

    private $siteModel;
    private $siteClassCache;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->siteModel      = new SiteModel();
        $this->siteClassCache = cache('site_class');
    }

    /**
     * 店铺首页
     * @return mixed
     */
    public function index()
    {
        $map        = [];
        $query      = $this->request->request();
        $searchKey  = $this->request->param('searchKey');
        $searchText = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey]     = ['like', '%' . $searchText . '%'];
        }
        $list = $this->siteModel->where($map)->order('site_id desc')->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 添加店铺
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'SiteValidate');
            if (true !== $result) {
                $this->error($result);
            }
            $res = $this->siteModel->addData($data);
            if ($res === false) {
                $this->error($this->siteModel->getError());
            } else {
                $this->success('操作成功！', url('index'));
            }
        }
        $this->assign('siteClass', $this->siteClassCache);
        return $this->fetch();
    }

    /**
     * 修改数据
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'SiteValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->siteModel->editData($data)) {
                $this->success('修改成功！', url('index'));
            } else {
                $this->error($this->siteModel->getError());
            }
        }
        $siteId = $this->request->param('site_id');
        $info   = $this->siteModel->get($siteId);
        $this->assign('info', $info);
        $this->assign('siteClass', $this->siteClassCache);
        return $this->fetch();
    }

    /**
     * 修改店铺状态
     */
    public function setStatus()
    {
        $siteId     = $this->request->param('site_id');
        $siteStatus = $this->siteModel->where('site_id', $siteId)->value('site_status');
        if ($siteStatus == 2) {
            $changeSiteStatus = 1;
        }
        if ($siteStatus == 1) {
            $changeSiteStatus = 0;
        }
        if ($siteStatus == 0) {
            $changeSiteStatus = 1;
        }
        $this->siteModel->where('site_id', $siteId)->setField('site_status', $changeSiteStatus);
        $this->success('操作成功！');
    }

}
